//
//  LatestViewController.m
//  YunGou
//
//  Created by macbook on 16/5/16.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "LatestViewController.h"
#import "GoodsDetailViewController.h"
#import "ProductModel.h"
#import "NSObject+Tools.h"
#import "NowAnnouncedCell.h"
#import "AlreadyAnnouncedCell.h"
#import <MJRefresh.h>

@interface LatestViewController ()
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger page;

@property (nonatomic,assign) NSInteger count;
@property (nonatomic,assign) NSInteger currentCount;
@end

@implementation LatestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [NSMutableArray array];
    _page = 0;
    [self loadData];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 0;
        [self loadData];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _page ++;
        [self loadData];
    }];
}

- (void)loadData{
    _currentCount = 0;
    [NetWorkTool latestAnnounceWithStart:_page * 10 success:^(id object) {
        if ([object[@"code"] integerValue] == 1) {
            for (NSDictionary *dict in object[@"data"]) {
                ProductModel *announceModel = [ProductModel modelWithDict:dict];
                [_dataArray addObject:announceModel];
            }
        }
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *baseCell = nil;
    ProductModel *announceModel = _dataArray[indexPath.row];
    
    double timespan = announceModel.q_end_time - NowTime;
    
    if (timespan > 0) {
        NowAnnouncedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        cell.announceModel = announceModel;
        cell.block = ^{
            _currentCount ++;
            if (_currentCount == _count) {
                [self loadData];
            }
            
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        };
        baseCell = cell;
    }else{
        AlreadyAnnouncedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.announceModel = announceModel;
        baseCell = cell;
    }
    return baseCell;
}

-(void)notice:(id)sender{
    _page = 0;
    _dataArray = [NSMutableArray array];
    [self.tableView reloadData];
    [self loadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    GoodsDetailViewController *goodsDetailVC = SB(@"Common", @"GoodsDetailVC");
    ProductModel *announceModel = _dataArray[indexPath.row];
    double timespan = announceModel.q_end_time - NowTime;
    
    if (timespan > 0) {
        goodsDetailVC.detailType = NowAnnounce;
    }else{
        goodsDetailVC.detailType = HasAnnounce;
    }
    goodsDetailVC.productModel = announceModel;
    [self.navigationController pushViewController:goodsDetailVC animated:YES];
}


@end
